// pages/login/index.js
import{ login} from '../../utils/asyncwx.js'
const http = require('../../utils/http.js');
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:'',
    avatarUrl: '',
    province: '',
    city: '',
    gender:'',
    uid:null,
    canIUse: wx.canIUse('button.open-type.getUserInfo') //检查微信版本是否可用
  },
  async handleGetUserInfo(e){// 获取用户信息点击事件
    this.setData({
      nickName: e.detail.userInfo.nickName,
      avatarUrl: e.detail.userInfo.avatarUrl,
      province: e.detail.userInfo.province,
      city: e.detail.userInfo.city,
      gender: e.detail.userInfo.gender,      
    });
   
      let {code}=await login() //2.获取小程序登录成功后的code
     
      let data = {
        code: code
      }
     //3.发送请求获取用的token
      const url = 'wx/getopenid/'
      let res= await http.post(url,data)
     
      let uid = res.uid
      let role = res.role
      let session_key=res.sessionKey
     
     
      wx.setStorageSync('uid', uid)
      wx.setStorageSync('role', role)
      wx.setStorageSync('session_key', session_key)
      const {userInfo}=e.detail //解构获取用户信息
      this.editwxinfo()
     
      wx.setStorageSync("userInfo", userInfo) //将用户信息保存到缓存中
      wx.navigateBack({
        delta: 1
      });

  },
  async editwxinfo(){
    const uid=wx.getStorageSync("uid")
    let url = 'Client/editwxinfo/';
      let data = {
        id: uid,
        nickName: this.data.nickName,
        avatarUrl:  this.data.avatarUrl,
        province:  this.data.province,
        city:  this.data.city,
        gender:  this.data.gender,
      }
    let res= await http.post(url,data)  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})